package org.dromara.work.controller;

import java.util.List;
import java.util.Arrays;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;

import org.dromara.common.mybatis.core.page.PageQuery;

import org.dromara.work.domain.vo.BeianInfoVo;
import org.dromara.work.domain.bo.BeianInfoSaveBo;
import org.dromara.work.service.IBeianInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 备案信息
 *
 * @author Chen
 * @date 2024-01-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/beian/info")
public class BeianInfoController extends BaseController {

    private final IBeianInfoService iBeianInfoService;

    /**
     * 查询备案信息列表
     */
    @SaCheckPermission("beian:info:list")
    @GetMapping("/list")
    public TableDataInfo<BeianInfoVo> list(BeianInfoSaveBo bo, PageQuery pageQuery) {
        return iBeianInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出备案信息列表
     */
    @SaCheckPermission("beian:info:export")
    @Log(title = "备案信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BeianInfoSaveBo bo, HttpServletResponse response) {
        List<BeianInfoVo> list = iBeianInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "备案信息", BeianInfoVo.class, response);
    }

    /**
     * 获取备案信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("beian:info:list")
    @GetMapping("/{id}")
    public R<BeianInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iBeianInfoService.queryById(id));
    }

    /**
     * 新增备案信息
     */
    @Log(title = "备案信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @SaCheckPermission("beian:info:add")
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BeianInfoSaveBo bo) {
        return toAjax(iBeianInfoService.insertByBo(bo));
    }

    /**
     * 修改备案信息
     */
    @SaCheckPermission("beian:info:edit")
    @Log(title = "备案信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BeianInfoSaveBo bo) {
        return toAjax(iBeianInfoService.updateByBo(bo));
    }

    /**
     * 删除备案信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("beian:info:edit")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iBeianInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
